import { Global } from './../../common/global';
import { Component } from "@angular/core";
import { IonicPage, NavController, NavParams } from "ionic-angular";
import { NativeProvider } from "../../providers/native/native";
import { BusiProvider } from "../../providers/busi/busi";

/**
 * Generated class for the LoginPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: "page-login",
  templateUrl: "login.html"
})
export class LoginPage {
  username: any;
  password: any;
  type: any;
  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    private native: NativeProvider,
    private busi: BusiProvider
  ) {
    this.type = this.navParams.get("type");
  }

  ionViewDidLoad() {
    console.log("ionViewDidLoad LoginPage");
  }
  login() {
    if (this.username == null || this.username == "") {
      this.native.alert("请输入用户名！");
      return;
    }

    if (this.password == null || this.password == "") {
      this.native.alert("请输入密码！");
      return;
    }
    this.busi.login(this.username, this.password, this.type).subscribe(
      data => {
        localStorage.setItem(Global.token, data.token);
        localStorage.setItem(Global.usertype,this.type);
        this.navCtrl.setRoot("MaintabPage");
      },
      error => {}
    );
  }
  goToSetting() {
    this.navCtrl.push("SettingPage");
  }
}
